
--[[
'tick()' returns the current server time in "Unix Time Format". It is not proper Unix time. It's a very large number, and this number is affected by timezones as servers in ROBLOX are misconfigured and return the wrong time, based on the timezone that they are in.
You can divide this number out and get the current time, then compensate for timezones by adding up to 12 or subtracting up to 11. This number could be slightly different on the client than on the server, as it queries the operating system for 
the time, and is not synchronized across the network.

'time()' is a function in ROBLOX that returns the game time. 
This is the number of seconds that the server has been running, and is synchronized across the network, meaning it will be the same in local scripts as it is in server scripts. If you are doing anything that doesn't require accurate real-life time or saving it outside of the game instance, then this is the preferred method.
]]

local m = {}

--- Returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00) under current UTC time.
m.Time = os.time;
--- Return current Seconds(since startup. A float value).
m.Secs = os.clock();
m.Seconds = m.Secs;
--- Return current Milliseconds(since startup. An int value).
m.MSecs = function() return math.ceil(os.clock() * 1000) end
m.Milliseconds = m.MSecs;

_DaySeconds = 3600 * 24;
--- 一天的秒数
m.DaySeconds = _DaySeconds;

--- 一周的秒数
_WeekSeconds = _DaySeconds * 7;
m.WeekSeconds = _WeekSeconds;

--- 一月的秒数（按30天算）
_MonthSeconds = _DaySeconds * 30;
m.MonthSeconds = _MonthSeconds;

--- 将格林威治时间(os.time)转换为可读的日期
function m.TimeToDate(t)
	return os.date("%c", t);	
end
--- 返回两个时间间隔几分钟
--- t1, t2 是秒数，可以通过 os.time()获得，也可以通过 m.Seconds 获得
function m.MinuteSpan(t1, t2)
	local spanSecs = t2 - t1;
	return spanSecs / 60;
end

--- 返回两个时间间隔几小时
--- t1, t2 是秒数，可以通过 os.time()获得，也可以通过 m.Seconds 获得
function m.HourSpan(t1, t2)
	local spanSecs = t2 - t1;
	return spanSecs / 3600;
end

--- 返回两个时间间隔几天
--- t1, t2 是秒数，可以通过 os.time()获得，也可以通过 m.Seconds 获得
function m.DaySpan(t1, t2)
	local spanSecs = t2 - t1;
	return spanSecs / _DaySeconds;
end

--- 返回两个时间间隔几个星期
--- t1, t2 是秒数，可以通过 os.time()获得，也可以通过 m.Seconds 获得
function m.WeekSpan(t1, t2)
	local spanSecs = t2 - t1;
	return spanSecs / _WeekSeconds;
end

--- 返回两个时间间隔几个星期
--- t1, t2 是秒数，可以通过 os.time()获得，也可以通过 m.Seconds 获得
function m.MonthSpan(t1, t2)
	local spanSecs = t2 - t1;
	return spanSecs / _MonthSeconds;
end

------
--- 将一个秒数转换为：天，小时，分钟，秒。例如输入605，则输出: 10, 5 (10分钟 5秒)
function m.ExtractTime(time)
	local days = math.floor(time / 86400)
	local remaining = time % 86400
	local hours = math.floor(remaining/3600)
	remaining = remaining % 3600
	local minutes = math.floor(remaining/60)
	remaining = remaining % 60
	local seconds = remaining
	
	return days, hours, minutes, seconds;
end

function math.round(num)
    return math.floor(num + 0.5)
end

--- 将一个秒数转换为：天，小时，分钟，秒 的格式。例如输入605，则输出: 10m 5s
--- 注意：最多展示两级单位。例如，如果结果是: 1d 3h 20m 5s，那将只展示: 1d 3h
function m.FormatTime(time)
	local days, hours, minutes, seconds = m.ExtractTime(time);
	if days > 0 then
		return tostring(days)..'d '..hours..'h ';
	end
	if hours > 0 then
		return tostring(hours)..'h '..minutes..'m ';
	end
	if minutes > 0 then
		return tostring(minutes)..'m '..math.round(seconds)..'s';
	end
	
	return tostring(math.round(seconds)).."s";
end

return m;
